#include "TcpSever.hpp"
#include "Protocol.hpp"
#include <memory>
int main(int argc,char* args[])
{
    if(argc != 3)
    {
        logMessage(FATAL,"请输入三个参数，待运行程序，ip地址，端口号\n");
        exit(1);
    }
    string ip = args[1];
    uint16_t port = atoi(args[2]);
    Sock sock;
    int socket = sock.Socket();
    sock.Connect(socket,ip,port);
    string buffer;
    while(1)
    {
        char c;
        cout<<"请输入操作符:";
        cin>>c;
        Requst req(10,20,c);
        Respon res;
        cout<<"准备序列化···"<<endl;
        string ret = req.Serialize();
        cout<<"序列化完成，准备编码发送···"<<endl;
        ret = Encode(&ret);
        cout<<"编码完成，准备编码发送···"<<endl;
        if(Send(socket,ret) == false)
        {
            logMessage(ERROR,"客户端发送错误");
            continue;
        }
        cout<<"发送完毕"<<endl;
        
        string rec;
        while(1)
        {
            cout<<"准备接收"<<endl;
            rec = Decode(socket,&buffer);
            cout<<"解析字符串···";
            if(rec == "") continue;
            cout<<"解析成功"<<endl;
            res.DeSerialize(rec);
            cout<<"准备输出结果"<<endl;
            cout<<res._result<<endl;
            cout<<res._code<<endl;
            break;
        }
    }
    close(socket);
    return 0;
}